var BOARD_CATEGORY_MESSAGE = 'MESSAGE';
var BOARD_TYPE_PRIVATE = 'PRIVATE';

function sendBulkEmail(emails) {
	 
	 for (var i = 0; i < emails.length; i++) {
		 sendEmail(emails[i],$("#conversationText").val(),$("#messageLink").val());
		 console.log('sending to' + emails[i]);
	}
 }
function sendEmail(jsContact,strBody,strSubject) {
          
          var request = gapi.client.jb.sendMail({
            'body': strBody,
            'subject':strSubject, 
            'to':jsContact.username + '@facebook.com', 
            'from':'shahab@noranj.com'
          });
          request.execute(function(jsonResp, rawResp) {
        	  console.log(jsonResp);
              console.log(rawResp);
          });
        }

//************************************************************************************************************* 
function listUser() {
      var request = gapi.client.jb.user.list();
      request.execute(function(jsonResp, rawResp) {
        console.log(jsonResp);
          console.log(rawResp);
      });
    }

//*************************************************************************************************************
function addBoard(data,discussionData){
	 var request = gapi.client.jb.board.insert({
	 'boardCategory':BOARD_CATEGORY_MESSAGE ,
	 'boardDescription':data.boardDescription,
	 'boardItem':data.boardItem,
	 'boardKeywords':data.boardKeywords,
	 'boardName':data.boardName,
	 'boardTitle':data.boardTitile,
	 'boardType':BOARD_TYPE_PRIVATE ,
	 'dateCreated':data.dateCreated,
	 'friendIds':data.friendIds,
	 'merchantId':data.merchantId,
	 'userId':data.userId
	 });
	 request.execute(function(jsonResp, rawResp) {
		  $("#jbBoardId").val(jsonResp.id);
		  discussionData.boardItemId = jsonResp.id;
		  addBoardItemDiscussion(discussionData);
		  console.log(jsonResp.id);
     });
}

//*************************************************************************************************************
function addBoardItemDiscussion(data){
	 var request = gapi.client.jb.board.addDiscussion({
	 'boardId':data.boardItemId,
	 'date':data.date,
	 'discussion':data.discussion,
	 'faceBookReference':data.faceBookReference,
	 'userId':data.userId,
	 'status': 'NEW'
	 });
	 request.execute(function(jsonResp, rawResp) {
		 console.log(jsonResp.code);
    });
}

//*************************************************************************************************************  
function setAddUser(data,friends){
	console.log('Users added:');
	console.log(data);
	
	if(data!='undefined'){
		var request = gapi.client.jb.user.fb.get({
			'id':data.facebookId
		});
		 request.execute(function(jsonResp, rawResp) {
			
			//if (jsonResp.error_message!=undefined){  //TODO:SA:Change it to a way which it can get the exception code instead of null value.
			if(jsonResp.code==-32099){
				request = gapi.client.jb.user.insert({
		  			 'address': data.address,
		  			 'age':data.age,
		  			 'city':data.city,
		  			 'country':data.country,
		  			 'dateCreated':data.dateCreated,
		  			 'email':data.email,
		  			 'facebookId':data.facebookId,
		  			 'firstName':data.firstName,
		  			 'friendIds':friends,
		  			 'gender':data.gender,
		  			 'ipAddress':data.ipAddress,
		  			 'lastName':data.lastName,
		  			 'merchantId':data.merchantId,
		  			 'role': 'USER'
		  			 
		  		 });
		  		 request.execute(function(jsonResp, rawResp) {
		  			$("#jbUserId").val(jsonResp.id);
		  	  	  	console.log('User is inserted.' + jsonResp.id);
		  	    });
	  	  	}
			else
				$("#jbUserId").val(jsonResp.id);
	    });
	}
	
}

//*************************************************************************************************************  

function addFriend(id,name,username){
		var request = gapi.client.jb.user.fb.get({
			'id':id
		});
		 request.execute(function(jsonResp, rawResp) {
			if(jsonResp.code==-32099){
				request = gapi.client.jb.user.insert({
		  			 'address': '',
		  			 'age':'',
		  			 'city':'',
		  			 'country':'',
		  			 'dateCreated':'',
		  			 'email':username ,
		  			 'facebookId':id,
		  			 'firstName':'',
		  			 'gender':'',
		  			 'ipAddress':'',
		  			 'lastName':name,
		  			 'merchantId':$("#jbMerchantId").val()
		  		 });
		  		 request.execute(function(jsonResp, rawResp) {
		  			addUserToCookie(jsonResp,id);
		  	    });
	  	  	}
			else{
				addUserToCookie(jsonResp,id);
			}
	    });
}
//*************************************************************************************************************  

function getUID()
  {
       var dateObject = new Date();
       var uniqueId =
            dateObject.getFullYear() + '' +
            dateObject.getMonth() + '' +
            dateObject.getDate() + '' +
            dateObject.getTime();

       return uniqueId;
  };

     
  function addUserToCookie(data,fbid){
  	var date = new Date();
	date.setTime(date.getTime() + (60 * 1000))
	  
	var jsonContributorsList = {};
	var tempdata = {"data" : [{"id":data.id,"fbid":fbid}]};
	
	if($.cookie("contributorsList") == 'null'){
       $.cookie("contributorsList", JSON.stringify(tempdata),date);
    }
    else{
       jsonContributorsList = JSON.parse($.cookie("contributorsList"));
       jsonContributorsList.data.push(tempdata.data[0]);
       $.cookie("contributorsList", JSON.stringify(jsonContributorsList),date);
    }
  }

